<?php
/**
 * Created by PhpStorm.
 * User: 29620
 * Date: 2021/10/29
 * Time: 11:33
 */
namespace app\admin\controller;

use app\BaseController;
use app\admin\model\Brands;
use app\common\traits\Jump;
use think\facade\Db;
use think\facade\Filesystem;
use think\facade\View;
use think\facade\Request;
use think\facade\Cache;
use think\exception\ValidateException;

class Brand extends BaseController
{
    public function brandList()
    {
        $post = Request::param();
        try{
            validate(\app\admin\validate\Brand::class)->scene('select')->check($post);
        }catch (ValidateException $e){
            $this->error($e->getMessage());
        }
        $whereOr=[];
        $where=[];
        //关键字
        if (isset($post['keywords']) && !empty($post['keywords']))    $where['keywords'] = $post['keywords'];
        //推荐
        if (isset($post['recommended']) && $post['recommended']<>'')    $where['recommended'] = $post['recommended'];
        //显示
        if (isset($post['if_show']) && count($post['if_show'])==1)    $where['if_show'] = $post['if_show'];
        //有图无图
        if (isset($post['brand_logo']) && count($post['brand_logo'])==1)    $where['brand_logo'] = $post['brand_logo'][0];

        $pinp = Brands::getBrand($where,$post,$whereOr);

        View::assign('pinp',$pinp);
        View::assign($post);

        return view();
    }

    public function brandAdd()
    {
        if(Request::isPost()){
            $post = Request::post();
            try{
                validate(\app\admin\validate\Brand::class)->scene('add')->check($post);
            }catch (ValidateException $e){
                $this->error($e->getMessage());
            }

            list($res,$path) = Upload::upload('img');
            if($res) $post['brand_logo']=$path;
            $post['brand_logo']=$path;

            Db::startTrans();
            try{
                list($res,$msg) = Brands::AddInfo($post);
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
            }

            if ($res){
                Upload::removeImg($path);
            }

            if($res){
                $this->success($msg,url('Brand/brandList'));
            }else{
                $this->error($msg);
            }
        }else{
            return view();
        }
    }

    public function brandEdit()
    {
        if (Request::isPost()){
            //$id = Request::get('id');
            $post = Request::post();

            try{
                validate(\app\admin\validate\Brand::class)->scene('edit')->check($post);
            }catch (ValidateException $e){
                $this->error($e->getMessage());
            }
            list($res1,$path) = Upload::upload('img');
            if($res1) $post['brand_logo']=$path;
            $post['brand_logo']=$path;


            $if_show=Request::post('if_show',0);
            $recommended=Request::post('recommended',0);
            $post['if_show']=$if_show;
            $post['recommended']=$recommended;

            unset($post['jump']);

            $brand = Db::name('brand')->where('brand_id',$post['brand_id'])->find();
            list($res,$msg) = Brands::editInfo($post);
            if ($res1){
                Upload::removeImg($path);
            }
            if($res){
                $res3 = file_exists(app()->getRootPath().'public/static/admin/'.$brand['brand_logo']);
                if ($res && $res3 && $brand['brand_logo']) unlink(app()->getRootPath().'public/static/admin/'.$brand['brand_logo']);
                $post = Request::post();
                if ($post['jump'] == 1){
                    $this->success($msg,url('Brand/brandList',input('get.')));
                }else{
                    $this->success($msg,url('Brand/brandEdit',input('get.')));
                }
            }else{
                $this->error($msg);
            }

        }else{
            $id = Request::get('id');
            $edit = Brands::getInfo($id);
            View::assign('edit',$edit);
            return view();
        }
    }

    public function brandH()
    {
        $post = Request::param();

        $whereOr=[];
        $where=[];
        //关键字
        if (isset($post['keywords']) && !empty($post['keywords']))    $where['keywords'] = $post['keywords'];
        //推荐
        if (isset($post['recommended']) && $post['recommended']<>'')    $where['recommended'] = $post['recommended'];
        //显示
        if (isset($post['if_show']) && count($post['if_show'])==1)    $where['if_show'] = $post['if_show'];
        //有图无图
        if (isset($post['brand_logo']) && count($post['brand_logo'])==1)    $where['brand_logo'] = $post['brand_logo'][0];

        $pinp = Brands::geBrand($where,$post,$whereOr);

        View::assign('pinp',$pinp);
        View::assign($post);

        return view();
    }

    public function ajax()
    {
        if (Request::isPost()){
            $act = Request::post('act');
            $post = Request::post('brand_id');
            if($act=='软删'){
                $brand = Brands::destroy($post);
                if($brand>0){
                    echo '成功';
                    exit;
                }else{
                    echo '失败';
                    exit;
                }
            }
            if($act=='删除'){
                $del = Brands::destroy($post,true);
                if($del>0){
                    echo '成功';
                    exit;
                }else{
                    echo '失败';
                    exit;
                }
            }

            if($act=='还原'){
                $brand = Brands::onlyTrashed()->find($post);
                $res = $brand->restore();
                if($res>0){
                    echo '成功';
                    exit;
                }else{
                    echo '失败';
                    exit;
                }
            }

        }
        return \view();

    }
}









